package common.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wzd.springmvc.entity.UserEntity;

import common.login.RequestUtil;

public class PurviewFilter extends  HttpFilter  {
	private static final Logger Log = LoggerFactory.getLogger(PurviewFilter.class);
	
	@Override
	public void init(FilterConfig config) throws ServletException {
	}

	@Override
	public void destroy() {
	}

	@Override
	public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException{
		System.out.println("doFilter 过滤器开始");
        if ("GET".equalsIgnoreCase(request.getMethod())) {
            RequestUtil.saveRequest(request);
        }        
        String requestUri = request.getRequestURI();
        String contextPath = request.getContextPath();
        String url = requestUri.substring(contextPath.length());
        if ("/login.do".equals(url) || "/".equals(url)) {
        	filterChain.doFilter(request, response);
            return;
        } else {
        	UserEntity username = (UserEntity) request.getSession().getAttribute("user");
            if (username == null) {
                System.out.println("被拦截：跳转到login页面！");
                request.getRequestDispatcher("/login.html").forward(request, response);
            } else
            	filterChain.doFilter(request, response);
        }

	}

}
